package com.asktgapp.user.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.asktgapp.R;
import com.asktgapp.adapter.BrandAdapter;
import com.asktgapp.adapter.PutQuestionsChooseBrandAdapter;
import com.asktgapp.application.Apiservice;
import com.asktgapp.application.TestApiUtil;
import com.asktgapp.architecture.retrofit.ApiException;
import com.asktgapp.architecture.retrofit.ApiResponseBody;
import com.asktgapp.architecture.rxjava.ExceptionHandle;
import com.asktgapp.base.BaseFragment;
import com.asktgapp.eventbus.PayFinishEvent;
import com.asktgapp.eventbus.PutQuestionsChooseEvent;
import com.asktgapp.model.PutQuestionsChooseVO;
import com.asktgapp.modulebase.common.adapter.BaseAdapter;
import com.asktgapp.modulebase.common.adapter.BaseViewHolder;
import com.asktgapp.modulebase.common.util.Util;
import com.asktgapp.user.activity.PutQuestionsChooseModelActivity;
import com.asktgapp.utils.CustomPopWindow;
import com.asktgapp.widget.WordsNavigation;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.List;

import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

//提问选择设备品牌
public class PutQuestionsChooseBrandNewFragment extends BaseFragment {


    private PutQuestionsChooseVO putQuestionsChooseVO;
    private PutQuestionsChooseBrandAdapter mBAdapter;
    private ListView mBrandListView;
    private WordsNavigation mBrandWordNavigation;



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_put_qs_choose_brand, container, false);
        EventBus.getDefault().register(this);
        return super.onCreateView(inflater, viewGroup, savedInstanceState);
    }


    public static PutQuestionsChooseBrandNewFragment newInstance(PutQuestionsChooseVO data) {
        PutQuestionsChooseBrandNewFragment fragment = new PutQuestionsChooseBrandNewFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("data", data);
        fragment.setArguments(bundle);
        return fragment;
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void a(PayFinishEvent b) {
        if (b.getType()) {
            if (getActivity() != null) {
                getActivity().finish();
            }
        }
    }
    @Override
    protected void initView(View rootView) {
        getBaseActivity().setBackNavigationIcon();
        Bundle bundle = getArguments();
        putQuestionsChooseVO = (PutQuestionsChooseVO) bundle.getSerializable("data");

        mBAdapter = new PutQuestionsChooseBrandAdapter(getActivity(), new PutQuestionsChooseBrandAdapter.ItemCallback() {
            @Override
            public void onItemClick(String string, String id) {
                Intent intent = new Intent(getActivity(), PutQuestionsChooseModelActivity.class);
                putQuestionsChooseVO.setBrandId(id);
                putQuestionsChooseVO.setBrandName(string);
//                putQuestionsChooseVO.setBrandSort(bean.getBrandSort());
//                        if(putQuestionsChooseVO.isPutQuestion()){
                intent.putExtra("data", putQuestionsChooseVO);
                PutQuestionsChooseEvent.postEvent(putQuestionsChooseVO);
                startActivity(intent);
//                        }else {
//                        }
//                getBaseActivity().onBackPressed();
            }

            @Override
            public void onDataSetComplete() {
                dismissProgress();
            }

            @Override
            public void onStartSetData() {
                showProgress(getClass().getSimpleName());
            }
        });

        mBrandListView = rootView.getRootView().findViewById(R.id.list);
        mBrandWordNavigation = rootView.getRootView().findViewById(R.id.words);
        mBrandWordNavigation.setOnWordsChangeListener(new WordsNavigation.onWordsChangeListener() {
            @Override
            public void wordsChange(String words) {
                if (words.equals("#")) {
                    words = "热门品牌";
                }
                for (int i = 0; i < mBAdapter.getData().size(); i++) {
                    if (mBAdapter.getData().get(i).getHeadChar().equals(words)) {
                        mBrandListView.setSelection(i);
                        return;
                    }
                    String headerWord = mBAdapter.getData().get(i).getHeadChar().toUpperCase();
                    //将手指按下的字母与列表中相同字母开头的项找出来
                    if (words.equals(headerWord)) {
                        //将列表选中哪一个
                        mBrandListView.setSelection(i);
                        //找到开头的一个即可
                        return;
                    }
                }
            }
        });

        mBrandListView.setAdapter(mBAdapter);
        mBrandListView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (mBAdapter.getData().size() > 0) {
                    if (mBAdapter.getData().get(firstVisibleItem).getHeadChar().equals("热门品牌")) {
                        mBrandWordNavigation.setTouchIndex("#");
                    } else {
                        mBrandWordNavigation.setTouchIndex(mBAdapter.getData().get(firstVisibleItem).getHeadChar().toUpperCase());
                    }
                }
            }
        });

    }


    @Override
    public void loadData() {

        getBrandLlist();
    }

    private void getBrandLlist() {
        Apiservice apiservice = TestApiUtil.getInstance().create();
        HashMap params = new HashMap();
        params.put("typeId", putQuestionsChooseVO.getTypeId());
        Call<ApiResponseBody<List<PutQuestionsChooseVO>>> call = apiservice.GetBrandByType(params);
        call.enqueue(new Callback<ApiResponseBody<List<PutQuestionsChooseVO>>>() {
            @Override
            public void onResponse(Call<ApiResponseBody<List<PutQuestionsChooseVO>>> call, Response<ApiResponseBody<List<PutQuestionsChooseVO>>> response) {
                if (response.isSuccessful()) {
                    List<PutQuestionsChooseVO> list = response.body().getResult();
                    if (list.size() == 0) {
                        visibleNoData();
                        dismissProgress();
                    } else
                        mBAdapter.setData(list);
                } else {
                    showTost(response.raw().message(), 1);
                    dismissProgress();
                }
            }

            @Override
            public void onFailure(Call<ApiResponseBody<List<PutQuestionsChooseVO>>> call, Throwable t) {
                dismissProgress();
                ApiException apiException = ExceptionHandle.handleException(t);
                showTost(apiException.getMessage(), 1);

            }
        });

    }


}
